package com.yhgarden.bbs.controller.common;

import com.yhgarden.bbs.model.constants.YhbbsConstants;
import com.yhgarden.bbs.model.enums.StatusCode;
import com.yhgarden.bbs.model.helper.LoginHelper;
import com.yhgarden.bbs.model.response.BaseResponse;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = {"/common/security","*/common/security"})
public class SecurityController {

    @RequestMapping(value = "/getPublicKey", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getPublicKey(HttpSession session) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        //公钥一定是16位
        String publicKey = RandomStringUtils.randomAlphanumeric(16);
        //写入publicKey到session
        session.setAttribute(YhbbsConstants.PUBLIC_KEY, publicKey);
        Map<String,Object> data = new HashMap<>();
        data.put(YhbbsConstants.PUBLIC_KEY, publicKey);
        response.setData(data);
        return response;
    }

}
